//
//  MBLocalizableIamgeView.m
//  mZero-iOS
//
//  Created by catch on 16/7/14.
//  Copyright © 2016年 Shenzhen Maker Works Technology Co., Ltd. All rights reserved.
//

#import "MBLocalizableIamgeView.h"

@implementation MBLocalizableIamgeView

-(void)setLocalizableIamgeName:(NSString *)localizableIamgeName{
    _localizableIamgeName = localizableIamgeName;
    
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    NSArray * allLanguages = [defaults objectForKey:@"AppleLanguages"];
    NSString * preferredLang = [allLanguages objectAtIndex:0];
    
    NSString* strName = preferredLang;
    if ([[[[UIDevice currentDevice] systemVersion] substringToIndex:1] intValue]>=9) {
        NSArray* array = [preferredLang componentsSeparatedByString:@"-"];
        strName = [array firstObject];
        for (int i= 1; i< array.count -1;i++) {
            NSString* str = [array objectAtIndex:i];
            strName = [NSString stringWithFormat:@"%@-%@",strName,str];
        }
        
    }
    
    NSString* imageName = [NSString stringWithFormat:@"%@-%@",localizableIamgeName,strName];
    UIImage* image = [UIImage imageNamed:imageName];
    
    if (!image) {
        imageName = [NSString stringWithFormat:@"%@-en",localizableIamgeName];
        image = [UIImage imageNamed:imageName];
    }
    
    self.image = image;
    
    [self setNeedsDisplay];
}

@end
